<?php
    include_once("../../service/commentService.php");
    include_once("../../service/userService.php");
    
    $pro_id = $_GET["pro_id"];
    $page = $_GET["p"];
    $record_per_page = 5;
    $start_record = ($page * $record_per_page) - 4;
    $rootComments = getAllApproveFirstCommentOfProduct($pro_id,$start_record,$record_per_page);
?>
<?php
    while($rootComment = mysql_fetch_array($rootComments)){
?>
    <div class="comment-block-wrapper">
        <div class="comment-block">
            <div class="comment-block-header">
            	<div>
                <?php
                    $user = findUserById($rootComment["USER_ID"]);
                    $userInfo = mysql_fetch_array($user);
                    echo $userInfo["USER_FULLNAME"];
                ?>
                </div>
                <div>
                <?php
                    $creationDate = new DateTime($rootComment["COM_CREATIONDATE"]);
                    echo $creationDate->format('H:i d/m/Y ');                                              
                ?>                                            
                </div>
            </div>
            <div class="comment-content">
                <?php
                    echo $rootComment["COM_CONTENT"];                                            
                ?>
            </div>
        </div>
        <!--Reply-->
        <?php
                echo "<div class='comment-counter' style='cursor:pointer' onclick='displayReplyTxtField(".$rootComment["COM_ID"].");'>Trả lời";
                //if(hasChildComment($rootComment["COM_ID"])){
                //    echo "(";
                //    echo countChildComment($rootComment["COM_ID"]);
                //    echo ")";        
                //}
                echo "</div>";
                echo "<div id='div_".$rootComment["COM_ID"]."' style='display:none'>";
                echo "<textarea class='comment' id='txt_".$rootComment["COM_ID"]."' name='txt_".$rootComment["COM_ID"]."'></textarea>";
                echo "<a onclick='reply(".$rootComment["COM_ID"].");' class='send-comment-btn' href='#'>GỬI TRẢ LỜI</a>";
                echo "</div>";
                $childComments = getAllChildrenComment($rootComment["COM_ID"]);
                while($childComment = mysql_fetch_array($childComments)){
            ?>
                <div class="comment-block-wrapper">
                    <div class="comment-block">
                        <div class="comment-block-header">
                            <div>
                                <?php
                                    $user = findUserById($childComment["USER_ID"]);
                                    $userInfo = mysql_fetch_array($user);
                                    echo $userInfo["USER_FULLNAME"];
                                ?>
                            </div>
                            <div>
                            <?php
                                $creationDate = new DateTime($childComment["COM_CREATIONDATE"]);
                                echo $creationDate->format('H:i d/m/Y ');                                              
                            ?> 
                            </div>
                        </div>
                        <div class="comment-content">
                            <?php
                                echo $childComment["COM_CONTENT"];                                            
                            ?>
                        </div>
                    </div>
                </div>
            <?php
                }
            ?>
    </div>        
<?php                                        
    }
?>
